package com.aplink.generic.pojo;

import java.io.Serializable;
import java.util.ArrayList;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.aplink.generic.constant.type.SqlDataType;

public class ObjPropInfo implements Parcelable, Serializable {
	public static final Parcelable.Creator<ObjPropInfo> CREATOR = new Parcelable.Creator<ObjPropInfo>() {

		@Override
		public ObjPropInfo createFromParcel(final Parcel source) {
			return new ObjPropInfo(source);
		}

		@Override
		public ObjPropInfo[] newArray(final int size) {
			return new ObjPropInfo[size];
		}
	};
	private static final long serialVersionUID = 2801115972569108597L;
	private boolean isAuto;
	private boolean isCreatedTime;
	private boolean isDeviceID;
	private boolean isFKey;
	private boolean isGroupBy;
	private boolean isImageLocalPath;
	private boolean isImageServerPath;
	private boolean isKey;
	private boolean isLatitude;
	private boolean isLongitude;
	private boolean isMobileOS;
	private boolean isPassword;
	private boolean isSave = true;
	private boolean isSortAsc;
	private boolean isSortDesc;
	private boolean isSyncStatus;
	private boolean isToken;
	private boolean isUsername;
	private String mBackUpTable;
	private String mColumnName;
	private ArrayList<String> mDataRules;
	private String mDataType;
	private String mDefaultValue;
	private String mExpress;
	private String mForeignKeyId;
	private String mForeignTable;
	private String mJoinType;
	private TableRelationship mRelationship;
	private String mSort;
	private String mTableName;
	private String mTableRoot;
	private String mWebserviceVariable;

	public ObjPropInfo() {
		mDataRules = new ArrayList<>();
	}

	public ObjPropInfo(final ObjPropInfo propInfo) {
		this.isAuto = propInfo.isAuto;
		this.isFKey = propInfo.isFKey;
		this.isKey = propInfo.isKey;
		this.mTableRoot = propInfo.mTableRoot;
		this.mDataType = propInfo.mDataType;
		this.mSort = propInfo.mSort;
		this.mForeignKeyId = propInfo.mForeignKeyId;
		this.mForeignTable = propInfo.mForeignTable;
		this.mColumnName = propInfo.mColumnName;
		this.mWebserviceVariable = propInfo.mWebserviceVariable;
		this.isCreatedTime = propInfo.isCreatedTime;
		this.isImageServerPath = propInfo.isImageServerPath;
		this.isImageLocalPath = propInfo.isImageLocalPath;
		this.isGroupBy = propInfo.isGroupBy;
		this.mExpress = propInfo.mExpress;
		this.isSyncStatus = propInfo.isSyncStatus;
		this.mTableName = propInfo.mTableName;
		this.isSave = propInfo.isSave;
		this.mDefaultValue = propInfo.mDefaultValue;
		this.mRelationship = propInfo.getRelationship();
		mDataRules = propInfo.getDataRules();
	}

	public ObjPropInfo(final Parcel results) {
		results.readBooleanArray(new boolean[] { isAuto, isCreatedTime,
		        isDeviceID, isFKey, isGroupBy, isImageLocalPath,
		        isImageServerPath, isKey, isLatitude, isLongitude, isPassword,
		        isSave, isSyncStatus, isToken, isMobileOS, isUsername });
		results.readStringArray(new String[] { mBackUpTable, mColumnName,
		        mDataType, mDefaultValue, mExpress, mForeignKeyId,
		        mForeignTable, mSort, mTableName, mTableRoot,
		        mWebserviceVariable });
		mRelationship = TableRelationship.get(results.readString());
	}

	@Override
	public int describeContents() {
		return 0;
	}

	public String getAliasColumnName() {
		return this.mTableName + "_" + this.mColumnName;
	}

	public String getBackUpTable() {
		return this.mBackUpTable;
	}

	/**
	 * This property mapping for "op_id" attribute.
	 * 
	 * @return
	 */
	public String getColumnName() {
		return this.mColumnName;
	}

	public ArrayList<String> getDataRules() {
		return mDataRules;
	}

	public SqlDataType getDataType() {
		final SqlDataType dataType = SqlDataType.get(this.mDataType);
		if (dataType != null) {
			return dataType;
		}
		return null;
	}

	public String getDefaultValue() {
		return this.mDefaultValue;
	}

	public String getExpression() {
		return this.mExpress;
	}

	/**
	 * This property mapping for "iskey" attribute.
	 * 
	 * @return
	 */
	public String getFKeyID() {
		return this.mForeignKeyId;
	}

	public String getForeignFullColumnName() {
		if (TextUtils.isEmpty(this.mForeignKeyId)) {
			return this.mForeignTable + "." + this.mColumnName;
		}
		return this.mForeignTable + "." + this.mForeignKeyId;
	}

	public String getForeignKeyId() {
		return this.mForeignKeyId;
	}

	/**
	 * This property mapping for "f_om_id" attribute.
	 * 
	 * @return
	 */
	public String getForeignTable() {
		return this.mForeignTable;
	}

	public String getFullColumnName() {
		return String.format("%s.%s", this.mTableName, this.mColumnName);
	}

	public final String getJoinType() {
		return mJoinType;
	}

	public TableRelationship getRelationship() {
		return this.mRelationship;
	}

	public String getSort() {
		return this.mSort;
	}

	/**
	 * This property mapping for "om_id" attribute.
	 * 
	 * @return
	 */
	public String getTable() {
		return this.mTableName;
	}

	/**
	 * This property mapping for "p_om_id" attribute.
	 * 
	 * @return
	 */
	public String getTableRoot() {
		return this.mTableRoot;
	}

	public String getWebserviceVariable() {
		return this.mWebserviceVariable;
	}

	public boolean isAuto() {
		return this.isAuto;
	}

	public boolean isCreatedTime() {
		return this.isCreatedTime;
	}

	public boolean isDeviceID() {
		return this.isDeviceID;
	}

	public boolean isFKey() {
		return this.isFKey;
	}

	public boolean isGroupBy() {
		return this.isGroupBy;
	}

	public boolean isImageLocalPath() {
		return this.isImageLocalPath;
	}

	public boolean isImageServerPath() {
		return this.isImageServerPath;
	}

	public boolean isKey() {
		return this.isKey;
	}

	public final boolean isLatitude() {
		return isLatitude;
	}

	public final boolean isLongitude() {
		return isLongitude;
	}

	public boolean isMobileOS() {
		return isMobileOS;
	}

	public boolean isPassword() {
		return this.isPassword;
	}

	public boolean isSave() {
		return this.isSave;
	}

	public boolean isSort() {
		return this.mSort != null;
	}

	public final boolean isSortAsc() {
		return isSortAsc;
	}

	public final boolean isSortDesc() {
		return isSortDesc;
	}

	public boolean isSyncStatus() {
		return this.isSyncStatus;
	}

	public boolean isToken() {
		return this.isToken;
	}

	public boolean isUsername() {
		return this.isUsername;
	}

	public void setAuto(final boolean isAuto) {
		this.isAuto = isAuto;
	}

	public void setBackUpTable(final String backUpTable) {
		this.mBackUpTable = backUpTable;
	}

	public void setColumnName(final String opid) {
		this.mColumnName = opid;
	}

	public void setCreatedTime(final boolean isCreatedTime) {
		this.isCreatedTime = isCreatedTime;
	}

	public void setDataRules(ArrayList<String> dataRules) {
		mDataRules = dataRules;
	}

	public void setDataType(final String datatype) {
		this.mDataType = datatype;
	}

	public void setDefaultValue(final String defaultValue) {
		this.mDefaultValue = defaultValue;
	}

	public void setDeviceID(final boolean isDeviceID) {
		this.isDeviceID = isDeviceID;
	}

	public void setExpress(final String express) {
		this.mExpress = express;
	}

	public void setFKey(final boolean isFKey) {
		this.isFKey = isFKey;
	}

	public void setForeignKeyId(final String foreignKeyId) {
		this.mForeignKeyId = foreignKeyId;
	}

	public void setForeignObjectMapping(final String mFOm) {
		this.mForeignTable = mFOm;
	}

	public void setForeignObjectMappingId(final String foreignObjectMappingId) {
		this.mForeignTable = foreignObjectMappingId;
	}

	public void setForiegnKey(final String fkeyid) {
		this.mForeignKeyId = fkeyid;
	}

	public void setGroupBy(final boolean isGroupBy) {
		this.isGroupBy = isGroupBy;
	}

	public void setImageLocalPath(final boolean isImageLocalPath) {
		this.isImageLocalPath = isImageLocalPath;
	}

	public void setImageServerPath(final boolean isImageServerPath) {
		this.isImageServerPath = isImageServerPath;
	}

	public void setIsAuto(final boolean isAuto) {
		this.isAuto = isAuto;
	}

	public final void setJoinType(String joinType) {
		mJoinType = joinType;
	}

	public void setKey(final boolean isKey) {
		this.isKey = isKey;
	}

	public final void setLatitude(final boolean isLatitude) {
		this.isLatitude = isLatitude;
	}

	public final void setLongitude(final boolean isLongitude) {
		this.isLongitude = isLongitude;
	}

	public void setMobileOS(boolean isMobileOS) {
		this.isMobileOS = isMobileOS;
	}

	public void setPassword(final boolean isPassword) {
		this.isPassword = isPassword;
	}

	public void setRelationship(final TableRelationship relationship) {
		this.mRelationship = relationship;
	}

	public void setSave(final boolean isIgnore) {
		this.isSave = isIgnore;
	}

	public void setSort(final String sort) {
		this.mSort = sort;
	}

	public final void setSortAsc(boolean isSortAsc) {
		this.isSortAsc = isSortAsc;
	}

	public final void setSortDesc(boolean isSortDesc) {
		this.isSortDesc = isSortDesc;
	}

	public void setSyncStatus(final boolean isSyncStatus) {
		this.isSyncStatus = isSyncStatus;
	}

	public void setTableName(final String om) {
		this.mTableName = om;
	}

	public void setTableRoot(final String tableRoot) {
		this.mTableRoot = tableRoot;
	}

	public void setToken(final boolean isToken) {
		this.isToken = isToken;
	}

	public void setUsername(final boolean isUsername) {
		this.isUsername = isUsername;
	}

	public void setWebserviceVariable(final String webserviceVariable) {
		this.mWebserviceVariable = webserviceVariable;
	}

	@Override
	public String toString() {
		return getFullColumnName();
	}

	@Override
	public void writeToParcel(final Parcel dest, final int flags) {
		dest.writeBooleanArray(new boolean[] { isAuto, isCreatedTime,
		        isDeviceID, isFKey, isGroupBy, isImageLocalPath,
		        isImageServerPath, isKey, isLatitude, isLongitude, isPassword,
		        isSave, isSyncStatus, isToken, isMobileOS, isUsername });
		dest.writeStringArray(new String[] { mBackUpTable, mColumnName,
		        mDataType, mDefaultValue, mExpress, mForeignKeyId,
		        mForeignTable, mSort, mTableName, mTableRoot,
		        mWebserviceVariable });
		dest.writeString(mRelationship.getValue());
	}
}
